var express = require('express');
var router = express.Router();
var Typelist = require("../models/typelist");

var multer  = require('multer');
var upload = multer({ dest: 'upload/' })
var shangchuan = require('../models/uplodaTengxun_cos.js');


router.post('/addType', upload.array("image"),async function(req, res, next) {

    let param = {};

    if(req.files.length != 0){
        param["typePic"] = await shangchuan( req.files[0])
    }
    if(req.body.typename != ''){
        param["type"] = req.body.typename;
    }

    Typelist.findOne({type:req.body.typename},function(err1,result) {
        if (err1) {
            res.json({
                status: "1",
                msg: err1.message,
                result: ""
            })
        } else {
            if(result == null){
                Typelist.create( param ,function(err,doc){
                    if(err){
                        res.json({
                            status:"1",
                            msg:err.message,
                            result: ""
                        })
                    }else{
                        if(doc){
                            res.json({
                                status:"0",
                                msg:"添加成功",
                                result: "suc"
                            })
                        }
                    }
                })
            }else {
                res.json({
                    status:"1",
                    msg:"分类名称已存在",
                    result: ""
                })
            }
        }
    })
});

//删除类型
router.post('/delType', function(req, res, next) {

    var id =  req.body.id;
    Typelist.remove({"_id":id}, function (err,doc){
        if(err){
            res.json({
                status:"1",
                msg:err.message,
                result: ""
            })
        }else{
            if(doc){
                res.json({
                    status:"0",
                    msg:"删除成功",
                    result: "suc"
                })
            }
        }
    });
});
//修改类型
router.post('/editType', upload.array("image"),async function(req, res, next) {

    var param = {};
    if(req.body.typename != ''){
        param['type'] = req.body.typename
    }
    if (req.files.length > 0){
        param["typePic"] = await shangchuan( req.files[0]);
    }
    Typelist.findOne({type:req.body.typename},function(err1,result) {
        if (err1) {
            res.json({
                status: "1",
                msg: err1.message,
                result: ""
            })
        } else {
            if (result == null || result._id == req.body.id) {
                Typelist.update({
                    _id:req.body.id
                },{
                    $set:param
                },function(err,doc){
                    if(err){
                        res.json({
                            status:"1",
                            msg:err.message,
                            result: ""
                        })
                    }else{
                        if(doc){
                            res.json({
                                status:"0",
                                msg:"修改成功",
                                result: "suc"
                            })
                        }
                    }
                });
            }else {
                res.json({
                    status:"1",
                    msg:"分类名称已存在",
                    result: ""
                })
            }
        }
    })
    // var param = {
    //     type : req.body.typename,
    //     id :req.body.id,
    //     typePic:"http://localhost:3000/typelist/" + req.files[0].path
    // };
    // Typelist.update({
    //     _id:param.id
    // },{
    //     $set:{'typePic':param.typePic,"type":param.type}
    // },function(err,doc){
    //     if(err){
    //         res.json({
    //             status:"1",
    //             msg:err.message,
    //             result: ""
    //         })
    //     }else{
    //         if(doc){
    //             res.json({
    //                 status:"0",
    //                 msg:"修改成功",
    //                 result: "suc"
    //             })
    //         }
    //     }
    // });
});



//获取类型数据
router.get('/getType', function(req, res, next) {
    // let page = parseInt(req.param("page"));
    // let pageSize = parseInt(req.param("pageSize"));
    // let sort = req.param("sort");
    // let skip = (page-1)*pageSize;
    // let params = {};
    // let goodsModel = Goods.find(params).skip(skip).limit(pageSize);
    //
    // goodsModel.sort({"salePrice" : sort});
    var model = Typelist.find();
    model.exec(function(err,result){
        if(err){
            res.json({
                status:"1",
                msg:err.message
            })
        }else{

            res.json({
                status:"0",
                msg:"",
                result:{
                    count:result.length,
                    list:result
                }
            })
        }
    });
});

module.exports = router;
